package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.bean.BlogType;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：粮库左侧的分类适配器
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/1 16:26
 * 修改备注：
 */
public class LiangkuAssort1Adapter extends RecyclerView.Adapter<LiangkuAssort1Adapter.ViewHolder> {

    private Context mContext;
    private ArrayList<BlogType> types;
    private OnItemClickListener itemClickListener;

    public LiangkuAssort1Adapter(Context context, ArrayList<BlogType> types, OnItemClickListener itemClickListener) {
        this.mContext = context;
        this.types = types;
        this.itemClickListener = itemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_liangku_assort_1, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return types.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.line)
        View line;
        @Bind(R.id.iv_assort)
        ImageView ivAssort;
        @Bind(R.id.tv_assort)
        TextView tvAssort;

        int position = -1;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.onClickListener(position);
                }
            });
        }

        public void setData(int position) {
            this.position = position;
            BlogType type = types.get(position);
            tvAssort.setText(type.getName());
            if (type.isChecked()) {
                line.setVisibility(View.VISIBLE);
                tvAssort.setTextColor(mContext.getResources().getColor(R.color.theme_orange));
                if (!TextUtils.isEmpty(type.getImgurl2()))//有些分类没有点击后的图片，此时依然显示之前的图片
                    ImageUtils.loadAssort(mContext, type.getImgurl2(), ivAssort);
                else
                    ImageUtils.loadAssort(mContext, type.getImgurl(), ivAssort);
            }else {
                line.setVisibility(View.INVISIBLE);
                tvAssort.setTextColor(mContext.getResources().getColor(R.color.text_black));
                ImageUtils.loadAssort(mContext, type.getImgurl(), ivAssort);
            }
        }
    }
}
